﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;

namespace MicropleSoft.WmsApp.Base.DataBinding
{
    public class Binder
    {
        public static void BindingLookUpEditYesOrNoInt(LookUpEdit lookUpEdit)
        {
            InitBindingLookUpEdit(lookUpEdit);
            var list = YesOrNoInt.GetList();
            lookUpEdit.Properties.DataSource = list;
        }

        public static void BindingLookUpEditYesOrNoString(LookUpEdit lookUpEdit)
        {
            InitBindingLookUpEdit(lookUpEdit);
            var list = YesOrNoString.GetList();
            lookUpEdit.Properties.DataSource = list;
        }

        public static void BindingLookUpEditUomType(LookUpEdit lookUpEdit)
        {
            InitBindingLookUpEdit(lookUpEdit);
            var list = UomType.GetList();
            lookUpEdit.Properties.DataSource = list;
        }

        public static void BindingGridViewLookUpEditUomType(GridView gv, GridColumn gc)
        {
            if (gc == null)
                return;
            var repositoryItemLookUpEdit = new RepositoryItemLookUpEdit { AutoHeight = false };

            repositoryItemLookUpEdit.Columns.AddRange(new[]
                                                                    {
                                                                         new LookUpColumnInfo("Code", "代码"),
                                                                        new LookUpColumnInfo("Name", "名称")
                                                                    });
            repositoryItemLookUpEdit.DisplayMember = "Name";
            repositoryItemLookUpEdit.Name = "repositoryItemLookUpEdit";
            repositoryItemLookUpEdit.ValueMember = "Code";
            repositoryItemLookUpEdit.NullText = "";
            gv.GridControl.RepositoryItems.AddRange(
                new RepositoryItem[] { repositoryItemLookUpEdit });

            repositoryItemLookUpEdit.DataSource = UomType.GetList();
           
            gc.ColumnEdit = repositoryItemLookUpEdit;
        }

        private static void InitBindingLookUpEdit(LookUpEdit lookUpEdit)
        {
            var itemLookUpEdit = lookUpEdit;
            itemLookUpEdit.Properties.TextEditStyle = TextEditStyles.Standard;
            itemLookUpEdit.Properties.Columns.AddRange(new[]
                                                           {
                                                               new LookUpColumnInfo("Name", "名称")
                                                           });
            itemLookUpEdit.Properties.DisplayMember = "Name";
            itemLookUpEdit.Name = "LookUpEditProvider";
            itemLookUpEdit.Properties.ValueMember = "Code";
            itemLookUpEdit.Properties.NullText = "";
        }

        public static void BindingGridViewLookUpEditYesOrNo(GridView gv, GridColumn gc,string type)
        {
            if (gc == null)
                return;

            var repositoryItemLookUpEditAliveState = new RepositoryItemLookUpEdit { AutoHeight = false };

            repositoryItemLookUpEditAliveState.Columns.AddRange(new[]
                                                                    {
                                                                        new LookUpColumnInfo("Name", "名称")
                                                                    });
            repositoryItemLookUpEditAliveState.DisplayMember = "Name";
            repositoryItemLookUpEditAliveState.Name = "repositoryItemLookUpEditAliveState";
            repositoryItemLookUpEditAliveState.ValueMember = "Code";
            repositoryItemLookUpEditAliveState.NullText = "";
            gv.GridControl.RepositoryItems.AddRange(
                new RepositoryItem[] { repositoryItemLookUpEditAliveState });
            if(type == "int")
            {
                repositoryItemLookUpEditAliveState.DataSource = YesOrNoInt.GetList();
            }
            else if (type == "string")
            {
                repositoryItemLookUpEditAliveState.DataSource = YesOrNoString.GetList();
            }
            gc.ColumnEdit = repositoryItemLookUpEditAliveState;
        }
    }
}
